﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Regions;
using NavigationModule.Controllers;
using NavigationModule.Services;
using NavigationModule.ViewModels;

namespace NavigationModule.Views
{
    /// <summary>
    /// Interaction logic for LeftSideNavigationView.xaml
    /// </summary>
    public partial class LeftSideNavigationView : UserControl, INavigationAware
    {
        private LeftSideNavigationViewModel Model;
        private readonly LeftSideNavigationService _leftSideService;
        private readonly LeftSideNavigationController _leftSideController;

        public LeftSideNavigationView()
        {
            InitializeComponent();

            _leftSideService = new LeftSideNavigationService();
            _leftSideController = new LeftSideNavigationController();
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false; //throw new NotImplementedException();
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            //throw new NotImplementedException();
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            Model = _leftSideService.Get(Infrastucture.NavigationMenu.Settings);
            this.DataContext = Model;
        }

        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            navigate(sender as DataGrid);
        }

        private void DataGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            navigate(sender as DataGrid);
        }

        private void navigate(DataGrid grid)
        {
            if (grid.SelectedItem != null)
            {
                var link = grid.SelectedItem as Infrastucture.Navigation.NavigationLink;
                _leftSideController.NavigateFrom(link);
            }
        }
    }
}
